<template>
  <div class="bg">
    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="addcommodity">上传Banner</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :header-cell-style="thStyleFun"
        :cell-style="cellStyleFun"
      >
        <el-table-column prop="index" width="100" label="序号"></el-table-column>
        <el-table-column prop="date" label="首页Banner">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.bannerImage"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="跳转类型">
          <template slot-scope="scope">
            <span>{{scope.row.bannerType==1?'站内':'站外'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="bannerCreateTime" label="时间"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.bannerStatus==1?'上架':'下架'}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="320">
          <template slot-scope="scope">
            <el-button
              @click="handleClickTop(scope.row)"
              type="primary"
              size="small"
            >{{scope.row.bannerSort==9999?'取消':'置顶'}}</el-button>
            <el-button @click="compile(scope.row)" type="warning" size="small">编辑</el-button>
            <el-button
              @click="updownClick(scope.row)"
              type="primary"
              size="small"
            >{{scope.row.bannerStatus==1?'下架':'上架'}}</el-button>
            <el-button @click="deletClick(scope.row)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        style="margin-top:24px"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
export default {
  data() {
    return {
      downloadLoading: false,
      tableData: [], //获取列表
      current: 1, //获取页数
      total: 0 //获取总条数
    };
  },
  mounted() {
    this.bannerlist();
  },
  methods: {
    // 获取banner列表
    bannerlist() {
      let param = {
        size: 10,
        current: this.current
      };
      this.$http.article.bannerList(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取banner列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 置顶
    handleClickTop(val) {
      let sortMessage = "";
      if (val.bannerSort == 0) {
        sortMessage = "置顶";
      } else {
        sortMessage = "取消置顶";
      }
      this.$confirm(`此操作将${sortMessage}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            bannerId: val.bannerId,
            bannerSort: val.bannerSort == 9999 ? 0 : 9999
          };
          console.log(params, "params");
          this.$http.article.addBanner(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg
              });
              this.bannerlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });

      console.log(val);
    },
    // 编辑
    compile(val) {
      this.$router.push({
        path: "/home/updateBanner",
        query: { bannerId: val.bannerId }
      });
    },
    // 删除
    deletClick(val) {
      console.log(val);
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.article
            .bannerDelet({ pet_bannerId: val.bannerId })
            .then(res => {
              if (res.code == 200) {
                /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

                //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
                const totalPage = Math.ceil((this.total - 1) / 10);

                console.log("totalPage", totalPage);

                //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
                const pagenum =
                  this.current > totalPage ? totalPage : this.current;

                console.log("获取总条数", pagenum);

                //避免pagenum变为0
                this.current = pagenum < 1 ? 1 : pagenum;
                // 提示弹窗
                this.$message.success("删除成功!");
                this.bannerlist();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.current = val;
      this.bannerlist();
      console.log(val);
    },
    //上下架
    updownClick(val) {
      let emssage = "";
      if (val.bannerStatus == 1) {
        emssage = "是否下架";
      } else {
        emssage = "是否上架";
      }
      this.$confirm(`此操作将${emssage}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let status = "";
          if (val.bannerStatus == 1) {
            status = 2;
          } else {
            status = 1;
          }
          let params = {
            bannerId: val.bannerId,
            bannerStatus: status
          };
          this.$http.article.addBanner(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg
              });
              this.bannerlist();
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
              this.bannerlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消成功"
          });
        });
    },
    // 居中表格
    thStyleFun() {
      return "text-align:center";
    },
    cellStyleFun() {
      return "text-align:center";
    },
    // 新增banner
    addcommodity() {
      this.$router.push("/home/updateBanner");
      console.log("添加礼品");
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  // overflow: hidden;
  padding-top: 10px;
}
.box-card {
  width: 98%;
  height: 80px;
  margin: 0 auto;
}
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>